﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemSettings.InstituTions;
using SystemCenterSoa.IRepository.SystemSettings.InstituTions;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;
using SystemCenterSoa.Repository.CommonLib;
using System.Data;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

namespace SystemCenterSoa.Repository.SystemSettings.InstituTions
{
    public class DepartmentRepository : BaseRepository, IDepartmentRepository
    {
        //注入ef的的上下文
        public DepartmentRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }
        public List<DepartmentOfTypePo> BindDepType()
        {
            //return _systemCenterDbSoaContext.AdminUserPo.FirstOrDefault(c => c.user_name == userName);
            return _systemCenterDbSoaContext.DepartmentOfTypePo.ToList();
        }

        public bool DepartmentAdd(DepartmentOfManagementPo view)
        {
            view.dep_isshow = 1;
            Random ran = new Random();
            view.dep_no = ran.Next(100, 999).ToString();
            _systemCenterDbSoaContext.DepartmentOfManagementPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool DepartmentDelete(string ids)
        {
            string[] arr = ids.Split(',');
            foreach (var item in arr)
            {
                int id = Convert.ToInt32(item);
                var list = _systemCenterDbSoaContext.DepartmentOfManagementPo.Find(id);
                _systemCenterDbSoaContext.DepartmentOfManagementPo.Remove(list);
            }
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool DepartmentUpdate(DepartmentOfManagementPo view)
        {
            _systemCenterDbSoaContext.Entry(view).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool DepartmentUpdateById(int id)
        {
            var list = _systemCenterDbSoaContext.DepartmentOfManagementPo.Find(id);
            if (list.dep_isshow == 0)
            {
                list.dep_isshow = 1;
            }
            else
            {
                list.dep_isshow = 0;
            }
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public DepartmentOfManagementPo GetDepartmentById(int id)
        {
            return _systemCenterDbSoaContext.DepartmentOfManagementPo.FirstOrDefault(c => c.dep_id == id);
        }

        public PagerResult<DepartmentOfManagementPo> SearchPage(SearchModel searchModel)
        {
            var query = _systemCenterDbSoaContext.DepartmentOfManagementPo.AsQueryable();
            //判断有没有搜索条件
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                //把搜索条件拿出来
                Dictionary<string, string> tempDi = searchModel.SearchItem;
                //tempDi["user_name"]
                //搜索字段为user_name  
                string depName = "depname";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(depName) && !string.IsNullOrEmpty(tempDi[depName]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    query = query.Where(c => c.dep_name.Contains(tempDi[depName]));
                }
                if (!string.IsNullOrEmpty(searchModel.OrderBy))
                {
                    if (searchModel.OrderBy == "dep_order")
                    {
                        if (searchModel.OrderByType == OrderByType.ASC)
                        {
                            query = query.OrderBy(c => c.dep_order);
                        }
                        else
                        {
                            query = query.OrderByDescending(c => c.dep_order);
                        }
                    }
                }
            }
            return BasePageSearch(query, searchModel);
        }
    }
}
